package com.goparry.taskman.restclient;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.res.Resources;
import android.util.Xml;

import com.goparry.taskman.repository.*;


public class XmlPullRObjectsParser {
	
	XmlPullParser xpp;
	
	public XmlPullRObjectsParser(String UrlString) {
		xpp = Xml.newPullParser();
		if (UrlString.startsWith("<RepositoryObjects")) {
	        try {
	            xpp.setInput(new InputStreamReader(new ByteArrayInputStream(UrlString.getBytes())));
	        } catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}

			
		} else {
			try {
				URL url= new URL(UrlString);
				xpp.setInput(url.openConnection().getInputStream(), null) ; 
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}

	}
	public XmlPullRObjectsParser(Resources resources, int id) {
		xpp = resources.getXml(id);
	}	
	
	public List<RObject> parse() {
		List<RObject> rObjects = null;
		
		try {
			int eventType = xpp.getEventType();
			RObject currentRObject = null;
			boolean done = false;
			while (eventType != XmlPullParser.END_DOCUMENT && !done) {
				String name = null;
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					rObjects = new ArrayList<RObject>();
					break;
				case XmlPullParser.START_TAG:
					name = xpp.getName();
					if (name.equalsIgnoreCase("RepositoryObject")) {
						currentRObject = new RObject();
					} else if (currentRObject != null) {
						if (name.equalsIgnoreCase("Name")) {
							currentRObject.setName(xpp.nextText());
						} else if (name.equalsIgnoreCase("Title")) {
							currentRObject.setTitle(xpp.nextText());
						} else if (name.equalsIgnoreCase("Revision")) {
							currentRObject.setRevision(xpp.nextText());
						} else if (name.equalsIgnoreCase("Status")) {
								currentRObject.setStatus(xpp.nextText());
						} else if (name.equalsIgnoreCase("Format")) {
							currentRObject.setFormat(xpp.nextText());
						} else if (name.equalsIgnoreCase("Size")) {
							currentRObject.setSizeAsString(xpp.nextText());
						} else if (name.equalsIgnoreCase("ID")) {
							currentRObject.setID(xpp.nextText());
						} else if (name.equalsIgnoreCase("DateLastModified")) {
							currentRObject.setDateLastModified(xpp.nextText());
						} else if (name.equalsIgnoreCase("HTMLProperties")) {
							String html = xpp.nextText();
							currentRObject.setHtmlProperties(html);
						} else if (name.equalsIgnoreCase("IsContainer")) {
							String value = xpp.nextText();
							if (value.equalsIgnoreCase("TRUE")) {
								currentRObject.setIsContainer(true);
							} else {
								currentRObject.setIsContainer(false);
							}
						}
					}
					break;
				case XmlPullParser.END_TAG:
					name = xpp.getName();
					if (name.equalsIgnoreCase("RepositoryObject") && currentRObject != null) {
						rObjects.add(currentRObject);
					} else if (name.equalsIgnoreCase("RepositoryObjects")) {
						done = true;
					}
					break;
				}
				eventType = xpp.next();
			}
			if (rObjects.isEmpty()) {
				throw new RuntimeException("No repository objects were returned.");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return rObjects;
	}
}
